package com.example.stlink.utils.activityUtil;

import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager2.widget.ViewPager2;

import com.example.stlink.R;
import com.example.stlink.configs.adapter.ViewPagerAdapter;

import java.util.List;

public class CourseDetailUtil {

    private AppCompatActivity appCompatActivity;
    private ViewPager2 viewPager2; // 视图
    private TextView tvCourseIntro;
    private TextView tvCourseClassList;
    private TextView tvCourseSelectedStu;

    public CourseDetailUtil(AppCompatActivity appCompatActivity){
        this.appCompatActivity = appCompatActivity;
    }

    public void initView(){
        // 视图翻页工具
        viewPager2 = appCompatActivity.findViewById(R.id.nav_course_detail_fragment);
        tvCourseIntro = appCompatActivity.findViewById(R.id.tv_course_intro);
        tvCourseClassList = appCompatActivity.findViewById(R.id.tv_course_class_list);
        tvCourseSelectedStu = appCompatActivity.findViewById(R.id.tv_course_selected_stu);

        tvCourseIntro.setClickable(true);
        tvCourseClassList.setClickable(true);
        tvCourseSelectedStu.setClickable(true);
        tvCourseIntro.setFocusable(true);
        tvCourseClassList.setFocusable(true);
        tvCourseSelectedStu.setFocusable(true);
    }

    /**
     * 将碎片与ViewPager2进行绑定，并监听viewPager2的滑动
     */
    public void initPage(List<Fragment> list){
        // 设置点击触发
        tvCourseIntro.setOnClickListener(v ->
                viewPager2.setCurrentItem(0));
        tvCourseClassList.setOnClickListener(v ->
                viewPager2.setCurrentItem(1));
        tvCourseSelectedStu.setOnClickListener(v ->
                viewPager2.setCurrentItem(2));
        // 注册页面改变的回调函数
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {  //  声明页面切换后的回调函数
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
            @Override
            public void onPageSelected(int position) { //根据滑动设置被选择的碎片
                switch (position){
                    case 0:{
                        tvCourseIntro.setSelected(true);
                        tvCourseClassList.setSelected(false);
                        tvCourseSelectedStu.setSelected(false);
                        tvCourseIntro.getPaint().setFakeBoldText(true);
                        tvCourseClassList.getPaint().setFakeBoldText(false);
                        tvCourseSelectedStu.getPaint().setFakeBoldText(false);
                        break;
                    }
                    case 1:{
                        tvCourseIntro.setSelected(false);
                        tvCourseClassList.setSelected(true);
                        tvCourseSelectedStu.setSelected(false);
                        tvCourseIntro.getPaint().setFakeBoldText(false);
                        tvCourseClassList.getPaint().setFakeBoldText(true);
                        tvCourseSelectedStu.getPaint().setFakeBoldText(false);
                        break;
                    }
                    case 2:{
                        tvCourseIntro.setSelected(false);
                        tvCourseClassList.setSelected(false);
                        tvCourseSelectedStu.setSelected(true);
                        tvCourseIntro.getPaint().setFakeBoldText(false);
                        tvCourseClassList.getPaint().setFakeBoldText(false);
                        tvCourseSelectedStu.getPaint().setFakeBoldText(true);
                        break;
                    }
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) { }
        });
        setupViewPager(viewPager2, list);
    }

    /**
     * 将碎片与viewPager2进行绑定
     * @param viewPager2 碎片集中管理类
     * @param list 碎片列表
     */
    private void setupViewPager(ViewPager2 viewPager2, List<Fragment> list) {
        FragmentManager fragmentManager = appCompatActivity.getSupportFragmentManager();
        // 将创建的碎片列表加入适配器中
        ViewPagerAdapter adapter = new ViewPagerAdapter(fragmentManager, appCompatActivity.getLifecycle(), list);
        // viewPager2设置适配器
        viewPager2.setAdapter(adapter);
        viewPager2.requestDisallowInterceptTouchEvent(true);
    }
}
